﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace VectorEditor
{
    public class EllipseTool : Tool
    {
        Point lastMouseDownPosition;

        public override void HandleMouseDown(IDocumentControl control, Point mousePosition, MouseButtons button)
        {
            lastMouseDownPosition = mousePosition;
        }

        public override void HandleMouseUp(IDocumentControl control, Point mousePosition, MouseButtons button)
        {
            PointF ellipsePoint1 = control.ScreenToDocumentCoordinates(lastMouseDownPosition);
            PointF ellipsePoint2 = control.ScreenToDocumentCoordinates(mousePosition);
            RectangleF rect = new RectangleF(Math.Min(ellipsePoint1.X, ellipsePoint2.X),
                                               Math.Min(ellipsePoint1.Y, ellipsePoint2.Y),
                                               Math.Abs(ellipsePoint1.X - ellipsePoint2.X),
                                               Math.Abs(ellipsePoint1.Y - ellipsePoint2.Y));
            EllipsePrimitive prim = new EllipsePrimitive(rect);
            InsertPrimitiveCommand command = new InsertPrimitiveCommand(prim);
            control.Controller.PushCommand(command);
        }

        public override void HandleMouseMove(IDocumentControl control, Point mousePosition, MouseButtons button)
        {
            if (button == MouseButtons.Left)
            {
                Rectangle rect = new Rectangle(Math.Min(lastMouseDownPosition.X, mousePosition.X),
                                               Math.Min(lastMouseDownPosition.Y, mousePosition.Y),
                                               Math.Abs(lastMouseDownPosition.X - mousePosition.X),
                                               Math.Abs(lastMouseDownPosition.Y - mousePosition.Y));
                var pen = new Pen(Color.Gray);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                pen.DashPattern = new float[] { 4.0f, 2.0f };
                control.Refresh(g => { g.DrawEllipse(pen, rect); });
            }
        }

		public override void DrawOverlay(Graphics g)
		{
			
		}
	}
}
